/**
 * Copyright &copy; 2016-2017 <a href="http://git.oschina.net/whoamien/backend_management">Backend Management</a> All rights reserved.
 */

package com.xw.app.model;

import java.util.List;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.Max;
import javax.validation.constraints.NotNull;

import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.xw.framework.base.dao.BaseUpdatableEntity;
import com.xw.util.ConvertUtils;

/**
 * The Class AcctUser.
 *
 * @author Xi Wei
 * @version 1.0
 * @since 1.0
 */
@Entity
@Table(name = "acct_user")
@DynamicUpdate(value=true)
@DynamicInsert(value=true)
public class AcctUser extends BaseUpdatableEntity implements java.io.Serializable{
	private static final long serialVersionUID = 5454155825314635342L;

	


	//可以直接使用: @Length(max=50,message="用户名长度不能大于50")显示错误消息
	//columns START
	private Long systemId;
	
	@NotBlank @Length(max=255)	
	@Column(name = "login_name", unique = true, nullable = false, insertable = true, updatable = true, length = 45)
	private java.lang.String loginName; //loginName
	@NotBlank @Length(max=250)
	
	@Column(name = "user_name", unique = false, nullable = false, insertable = true, updatable = true, length = 250)
	private java.lang.String userName; //userName
	@NotNull @Max(32767)
	
	@Column(name = "gender", unique = false, nullable = false, insertable = true, updatable = true, length = 5)
	private int gender; //gender
	@Length(max=255)
	
	@Column(name = "password", unique = false, nullable = true, insertable = true, updatable = true, length = 45)
	private java.lang.String password; //password
	@Length(max=45)
	
	@Column(name = "mobile_phone", unique = false, nullable = true, insertable = true, updatable = true, length = 45)
	private java.lang.String mobilePhone; //mobilePhone
	@Length(max=45)
	
	@Column(name = "home_phone", unique = false, nullable = true, insertable = true, updatable = true, length = 45)
	private java.lang.String homePhone; //homePhone
	@Length(max=45)
	
	@Column(name = "work_phone", unique = false, nullable = true, insertable = true, updatable = true, length = 45)
	private java.lang.String workPhone; //workPhone
	
	
	@Column(name = "last_login_date", unique = false, nullable = true, insertable = true, updatable = true, length = 0)
	private java.sql.Timestamp lastLoginDate; //lastLoginDate
	
	private String logo;
	
	@Length(max=65535)
	private String remarks;
	
	//columns END

	/**
	 * Gets the system id.
	 *
	 * @return the system id
	 */
	public Long getSystemId() {
		return systemId;
	}

	/**
	 * Sets the system id.
	 *
	 * @param systemId the new system id
	 */
	public void setSystemId(Long systemId) {
		this.systemId = systemId;
	}


	/**
	 * Gets the login name.
	 *
	 * @return the login name
	 */
	public java.lang.String getLoginName() {
		return this.loginName;
	}
	
	/**
	 * Sets the login name.
	 *
	 * @param value the new login name
	 */
	public void setLoginName(java.lang.String value) {
		this.loginName = value;
	}

	/**
	 * Gets the user name.
	 *
	 * @return the user name
	 */
	public java.lang.String getUserName() {
		return this.userName;
	}
	
	/**
	 * Sets the user name.
	 *
	 * @param value the new user name
	 */
	public void setUserName(java.lang.String value) {
		this.userName = value;
	}

	/**
	 * Gets the gender.
	 *
	 * @return the gender
	 */
	public int getGender() {
		return this.gender;
	}
	
	/**
	 * Sets the gender.
	 *
	 * @param value the new gender
	 */
	public void setGender(int value) {
		this.gender = value;
	}

	/**
	 * Gets the password.
	 *
	 * @return the password
	 */
	public java.lang.String getPassword() {
		return this.password;
	}
	
	/**
	 * Sets the password.
	 *
	 * @param value the new password
	 */
	public void setPassword(java.lang.String value) {
		this.password = value;
	}

	/**
	 * Gets the mobile phone.
	 *
	 * @return the mobile phone
	 */
	public java.lang.String getMobilePhone() {
		return this.mobilePhone;
	}
	
	/**
	 * Sets the mobile phone.
	 *
	 * @param value the new mobile phone
	 */
	public void setMobilePhone(java.lang.String value) {
		this.mobilePhone = value;
	}

	/**
	 * Gets the home phone.
	 *
	 * @return the home phone
	 */
	public java.lang.String getHomePhone() {
		return this.homePhone;
	}
	
	/**
	 * Sets the home phone.
	 *
	 * @param value the new home phone
	 */
	public void setHomePhone(java.lang.String value) {
		this.homePhone = value;
	}

	/**
	 * Gets the work phone.
	 *
	 * @return the work phone
	 */
	public java.lang.String getWorkPhone() {
		return this.workPhone;
	}
	
	/**
	 * Sets the work phone.
	 *
	 * @param value the new work phone
	 */
	public void setWorkPhone(java.lang.String value) {
		this.workPhone = value;
	}

	/**
	 * Gets the last login date.
	 *
	 * @return the last login date
	 */
	public java.sql.Timestamp getLastLoginDate() {
		return this.lastLoginDate;
	}
	
	/**
	 * Sets the last login date.
	 *
	 * @param value the new last login date
	 */
	public void setLastLoginDate(java.sql.Timestamp value) {
		this.lastLoginDate = value;
	}
	
	

	

	public String getRemarks() {
		return remarks;
	}

	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}

	/**
	 * Gets the logo.
	 *
	 * @return the logo
	 */
	public String getLogo() {
		return logo;
	}

	/**
	 * Sets the logo.
	 *
	 * @param logo the new logo
	 */
	public void setLogo(String logo) {
		this.logo = logo;
	}



	@ManyToMany
	@JoinTable(name = "acct_user_role", joinColumns = { @javax.persistence.JoinColumn(name = "user_id") }, inverseJoinColumns = { @javax.persistence.JoinColumn(name = "role_id") })
	@Fetch(FetchMode.SUBSELECT)
	@OrderBy("id")
	@JsonIgnore
	private List<AcctRole> roleList = Lists.newArrayList();
	
	/**
	 * Gets the role list.
	 *
	 * @return the role list
	 */
	public List<AcctRole> getRoleList() {
		return this.roleList;
	}

	/**
	 * Sets the role list.
	 *
	 * @param roleList the new role list
	 */
	public void setRoleList(List<AcctRole> roleList) {
		this.roleList = roleList;
	}
	
	/**
	 * Gets the role names.
	 *
	 * @return the role names
	 */
	@Transient
	public List<String> getRoleNames() {
		return ConvertUtils.convertElementPropertyToList(getRoleList(), "roleName");
	}
	
	/**
	 * Gets the menu list.
	 *
	 * @return the menu list
	 */
	@Transient
	public List<SysMenu> getMenuList() {
		Map<Long,SysMenu> menuMap = Maps.newLinkedHashMap();
		List<AcctRole> roleList = getRoleList();
		
		for(AcctRole role:roleList){
			List<SysMenu> menuList = role.getMenuList();
			for(SysMenu menu : menuList){
				menuMap.put(menu.getId(), menu);
			}
			
		}
		return Lists.newArrayList(menuMap.values());
	}
}

